package com.cabbagetofu.crowd.controller;


import com.atguigu.crowd.until.ResultEntity;
import com.cabbagetofu.crowd.entity.po.ProjectPO;
import com.cabbagetofu.crowd.entity.vo.DetailProjectVO;
import com.cabbagetofu.crowd.entity.vo.PortalProjectVO;
import com.cabbagetofu.crowd.entity.vo.PortalTypeVO;
import com.cabbagetofu.crowd.entity.vo.ProjectVO;
import com.cabbagetofu.crowd.service.api.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProjectProviderController {

    @Autowired
    private ProjectService projectService;

    @RequestMapping("/get/all/project/remote")
    public List<ProjectPO> getAllProjectRemote(){

        return projectService.getAllProject();
    }

    @RequestMapping("/get/detail/project/remote/{projectId}")
    public ResultEntity<DetailProjectVO> getDetailProjectVORemote(@PathVariable("projectId") Integer projectId){

        try {

            DetailProjectVO detailProjectVO = projectService.getDetailProjectVO(projectId);
            return ResultEntity.successWithData(detailProjectVO);
        } catch (Exception e) {

            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

    /**
     * 根据数据库中的的分类表，获取每个分类所对应的项目，最终展示在首页上
     * @return
     */
    @RequestMapping("/get/portal/type/and/project/data/remote")
    public ResultEntity<List<PortalTypeVO>> getPortalTypeProjectDataRemote(){

        try {

            List<PortalTypeVO> portalTypeVOList = projectService.getPortalTypeVO();
            return ResultEntity.successWithData(portalTypeVOList);
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }


    /**
     * 保存发起的所有项目数据
     * @param projectVO
     * @param id
     * @return
     */
    @RequestMapping("/save/projectVO/remote")
    public ResultEntity<String> saveProjectRemote(@RequestBody ProjectVO projectVO, @RequestParam("memberId") Integer id){

        try {

            projectService.saveProject(projectVO, id);
            return ResultEntity.successWithoutData();

        } catch (Exception e) {

            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }

}
